package com.good.dao;

import com.good.po.foodtable;
import com.good.utils.jdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;


public class foodTableDao {
	
	ResultSet rs = null;
	Connection conn = null;
	PreparedStatement ps = null;

	
	String[] head = {"id", "名字", "类型", "价格", "日期"};
	
	public int add(foodtable ft){
		int result = -1;

		try {
			conn = jdbcUtils.getConn();
			String sql = "insert foodtable VALUES (NULL, ?, ?, ?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, ft.getName());
			ps.setString(2, ft.getType());
			ps.setString(3, ft.getPrice());
			ps.setString(4, ft.getDate());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcUtils.close(conn, ps, rs);
		}
		
		return result;
	}
	
	public int delete(String id){
		int result = -1;

		try {
			conn = jdbcUtils.getConn();
			String sql = "delete from foodtable where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcUtils.close(conn, ps, rs);
		}
		
		return result;
	}
	
	public int update(foodtable ft){
		int result = -1;

		try {
			conn = jdbcUtils.getConn();
			String sql = "update foodtable set name=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, ft.getName());
			ps.setString(2, ft.getId());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcUtils.close(conn, ps, rs);
		}
		
		return result;
	}
	
	public ArrayList<foodtable> selectAll(){
		ArrayList<foodtable> list = new ArrayList<>();
		try {
			conn = jdbcUtils.getConn();
			String sql = "select * from foodtable";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				String id = rs.getString("id");
				String name = rs.getString("name");
				String type = rs.getString("type");
				String price = rs.getString("price");
				String date = rs.getString("date");
				foodtable ft = new foodtable(id, name, type, price, date);
				list.add(ft);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcUtils.close(conn, ps, rs);
		}
		
		return list;
	}
	
	public DefaultTableModel selectAllForTable(String searcName){
		DefaultTableModel dtm = new DefaultTableModel(null, head);
		try {
			conn = jdbcUtils.getConn();
			String sql = "";
			if(searcName!=null&&!searcName.equals("")){
				sql = "select * from foodtable where name like '%"+searcName+"%'";
			}else{
				sql = "select * from foodtable";
			}
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				String id = rs.getString("id");
				String name = rs.getString("name");
				String type = rs.getString("type");
				String price = rs.getString("price");
				String date = rs.getString("date");
				Vector<Object> v = new Vector<Object>();
				v.add(id);
				v.add(name);
				v.add(type);
				v.add(price);
				v.add(date);
				dtm.addRow(v);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcUtils.close(conn, ps, rs);
		}
		
		return dtm;
	}
	
	public static void main(String[] args){
		foodTableDao ftd = new foodTableDao();
		foodtable ft = new foodtable("426", "5555", "2", "398", "2023-11-12");
		//int i = ftd.add(ft);
		//int i = ftd.delete("436");
		//int i = ftd.update(ft);
		//System.out.println("==================:" + i);
		ArrayList<foodtable> list = ftd.selectAll();
		for(int i = 0; i<list.size(); i++){
			foodtable obj = list.get(i);
			System.out.println(obj.getName());
		}
	}
}
